package com.css.fxfzaqbz.modules.assess.repository.impl;

import com.css.fxfzaqbz.constants.YNEnum;
import com.css.fxfzaqbz.modules.assess.entity.BigCasualtiesEntity;
import com.css.fxfzaqbz.modules.assess.repository.BigCasualtiesRepository;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.jdbc.support.KeyHolder;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;

/**
 * @author y1fan
 * @create 2022-09-19 9:44
 */
@Repository
public class BigCasualtiesRepositoryImpl implements BigCasualtiesRepository {


    @Resource
    NamedParameterJdbcTemplate namedParameterJdbcTemplate;
    @Resource
    JdbcTemplate jdbcTemplate;

    @Override
    public BigCasualtiesEntity getBigCasualtiesByActiveId(String scRelation) {
        String sql="";
        sql="select u.* from sys_big_casualties u  where  u.bc_activeid='"+scRelation+"' and u.bc_del_flag = '"+ YNEnum.N.toString() +"'";
        BigCasualtiesEntity bigCasualtiesEntity = null;
        try {
            bigCasualtiesEntity = jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<>(BigCasualtiesEntity.class));
            return bigCasualtiesEntity;
        } catch (DataAccessException e) {
            return null;
        }
    }

    @Override
    public void saveCasualties(BigCasualtiesEntity bigCasualtiesEntity) {
        String sql = "insert into sys_big_casualties  (bc_id,bc_activeid,bc_opinion,bc_status,bc_createtime,bc_del_flag,bc_createuser,content)values(:bcId,:bcActiveId,:bcOpinion,:bcStatus,:bcCreateTime,:bcDelFlag,:bcCreateUser,:content)";

        SqlParameterSource source = new BeanPropertySqlParameterSource(bigCasualtiesEntity);
        //拿到新插入的主键值（i）
        KeyHolder keyHolder = new GeneratedKeyHolder();
        int i = namedParameterJdbcTemplate.update(sql, source, keyHolder);
    }

    @Override
    public void updateBigStatus(String bbId) {
        String sql = "update sys_big_casualties set" +
                " bc_status = '"+YNEnum.Y+"' " +
                " where bc_id = '" + bbId + "'";
        jdbcTemplate.update(sql);
    }

    @Override
    public void upStatus(String id) {
        String sql = "update sys_big_casualties set" +
                " bc_status = '"+2+"' " +
                " where bc_activeid = '" + id + "'";
        jdbcTemplate.update(sql);
    }
}
